﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace FileSharing
{
    public class Uploads
    {
        private List<UploadTask> UploadList = new List<UploadTask>();
        private Diretorio Directory;

        Mutex UploadMut = new Mutex();

        public Uploads(Diretorio dir)
        {
            Directory = dir;
        }

        public void AddUploadTask(UploadTask upload)
        {
            UploadMut.WaitOne();
            UploadList.Add(upload);
            UploadMut.ReleaseMutex();
        }

        public void UploadCompleted(UploadTask upload)
        {
            UploadMut.WaitOne();
            UploadList.Remove(upload);
            UploadMut.ReleaseMutex();
        }

        public void Close()
        {
            foreach (UploadTask t in UploadList)
                t.Close();
        }

        public LocalFile FindLocalFile(RemoteFile file)
        {
            return Directory.FindFile(file.Hash, file.Length);
        }

        public UploadItem[] GetUploadCollection()
        {
            UploadItem[] col = new UploadItem[UploadList.Count];

            for (int i = 0; i < UploadList.Count; i++)
            {
                col[i] = UploadList[i].GetUploadInfo();
            }

            return col;
        }
    }
}
